package com.hongdee.scm.appearance.service.impl;

import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.appearance.dao.ITScmAppearanceDao;
import com.hongdee.scm.appearance.entity.TScmAppearance;
import com.hongdee.scm.appearance.service.ITScmAppearanceService;
import com.hongdee.scm.common.service.impl.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 名  称:TScmAppearanceServiceImpl
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.appearance.service.impl
 * 作  者: 术洪超
 * 日  期:2017-05-14 09:20
 */
@Service
public class TScmAppearanceServiceImpl implements ITScmAppearanceService {

    String tabName = "T_SCM_APPEARANCE";
    @Autowired
    private ITScmAppearanceDao itScmAppearanceDao;

    /**
     * @param uid            登录用户 ID
     * @param start          起始记录
     * @param limit          页面大小
     * @param appearanceCode 内饰编码
     * @param appearanceName 内饰名称
     * @param carSeriesId    车系id
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @Override
    public Map page(String start, String limit, String appearanceCode, String appearanceName, String carSeriesId, String uid) throws Exception {

        Common common = new Common();
        Boolean hasWhere = false;

        List params = new ArrayList();
        StringBuffer sql = new StringBuffer();
        sql.append("select * from " + tabName);

        if (!StringUtils.isEmpty(carSeriesId) && !carSeriesId.equals("")) {
            hasWhere = common.appendWhereIfNeed(sql, hasWhere);
            sql.append(" car_series_id = ?");
            params.add(carSeriesId);

        }

        if (!StringUtils.isEmpty(appearanceCode) && !appearanceCode.equals("")) {
            hasWhere = common.appendWhereIfNeed(sql, hasWhere);
            sql.append(" appearance_code like ?");
            params.add("%" + appearanceCode + "%");
        }
        if (!StringUtils.isEmpty(appearanceName) && !appearanceName.equals("")) {
            hasWhere = common.appendWhereIfNeed(sql, hasWhere);
            sql.append(" appearance_name like ?");
            params.add("%" + appearanceName + "%");
        }
        sql.append(" order by create_time desc");
        return this.itScmAppearanceDao.page(sql.toString(), params.toArray(), start, limit);
    }

    /**
     * @param data 要保存的实体
     * @return
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */

    @Override
    public TScmAppearance save(TScmAppearance data, String uid) throws Exception {

        //id为空时则为添加
        if (data.getId() == null) {
            if (data.getAppearanceCode() != null) {
                if (existCode(data.getAppearanceCode(), data.getCarSeriesId())) {
                    throw new Exception("外观代码重复！");
                }
            }
            if (data.getAppearanceName() != null) {
                if (existName(data.getAppearanceName(), data.getCarSeriesId())) {
                    throw new Exception("外观名称重复！");
                }
            }
            data.setCreateUser(uid);
            data.setCreateTime(Calendar.getInstance().getTimeInMillis());
        } else {
            //id不为空时则为修改
            data.setUpdateUser(uid);
            data.setUpdateTime(Calendar.getInstance().getTimeInMillis());
        }
        //data.setCompanyId(TokenUtil.getCompanyId(uid));
        this.itScmAppearanceDao.saveOrUpdate(data);
        return data;
    }

    /**
     * @param uid 登录用户 ID
     * @param id  主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @Override
    public Map info(String id, String uid) throws Exception {
        StringBuffer sql = new StringBuffer();

        Common common = new Common();
        Boolean hasWhere = false;
        List params = new ArrayList();
        sql.append("select * from " + tabName);
        if (StringUtils.isEmpty(id) && id.equals("")) {
            throw new Exception("请选择外观");
        }
        hasWhere = common.appendWhereIfNeed(sql, hasWhere);
        sql.append(" id = ?");
        params.add(id);

        return this.itScmAppearanceDao.info(sql.toString(), params.toArray());
    }

  /*
    判断外观编码是否存在，用于保存时判断外观编码是否重复
     */

    private boolean existCode(String appearanceCode, String carSeriesId) throws Exception {

        List params = new ArrayList();
        StringBuffer sb = new StringBuffer();
        sb.append("select * from " + tabName + " where car_series_id = ?");
        params.add(carSeriesId);

        if (!appearanceCode.isEmpty() && !appearanceCode.equals("")) {
            sb.append(" and appearance_code = ? ");
            params.add(appearanceCode);
        }

        Map map = this.itScmAppearanceDao.page(sb.toString(), params.toArray(), null, null);
        return Integer.valueOf(map.get("total").toString()) > 0;

    }

    /*
        判断外观名称是否存在，用于保存时判断外观名称是否重复
         */
    private boolean existName(String appearanceName, String carSeriesId) throws Exception {

        List params = new ArrayList();
        StringBuffer sb = new StringBuffer();
        sb.append("select * from " + tabName + " where car_series_id = ?");

        params.add(carSeriesId);

        if (!appearanceName.isEmpty() && !appearanceName.equals("")) {
            sb.append(" and appearance_name = ? ");
            params.add(appearanceName);
        }

        Map map = this.itScmAppearanceDao.page(sb.toString(), params.toArray(), null, null);
        return Integer.valueOf(map.get("total").toString()) > 0;

    }

    /**
     * 外观下拉
     *
     * @param series
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List combo(String series, String uid) throws Exception {
        StringBuffer sql = new StringBuffer();
        List param = new ArrayList();
        sql.append("select id,appearance_code code,appearance_name from " + tabName + " where car_series_id = ? ");
                //.append("(select car_series_code from t_bc_car_series where id = ? )  ");
        param.add(series);
        List<Map<String, Object>> mapList = this.itScmAppearanceDao.list(sql.toString(), param.toArray());
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            //temp.put("id", map.get("id"));
            temp.put("label", map.get("appearanceName"));
            temp.put("value",map.get("code"));
            resList.add(temp);
        }
        return resList;

    }

    /**
     * 是否停用外观
     * @param appearanceIds
     * @param uid
     * @throws Exception
     */
    public void appearanceCancel(String[] appearanceIds,String uid) throws Exception{

        if(appearanceIds==null|| appearanceIds.length<=0){
            throw new Exception("请选择外观");
        }
         List<String> list= new ArrayList<>();
         list = Arrays.asList(appearanceIds);
         for (String id:list){
             this.itScmAppearanceDao.executeSQL("delete from t_scm_appearance where id=?",id);
         }

    }
}
